﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ResumeService;
using Avatar.Model;

public partial class UserControl_Position : System.Web.UI.UserControl
{
    int _currentPositionID;
    public int PositionID
    {
        get
        {
            return ViewState["position"] == null ? 0 : Convert.ToInt32(ViewState["position"].ToString());
        }
        set
        {
            _currentPositionID = value;
            if (_currentPositionID > 0)
            {
                var _position = PositionManager.GetPosition(_currentPositionID);

                ddl_Position1.SelectedValue=_position.FatherID.ToString();

                DropDownListHelper.Bind(ddl_Postion2,PositionManager.GetPositionList(_position.FatherID),"name","id");

                ddl_Postion2.SelectedValue = _currentPositionID.ToString();

                this.SetCurrentPosition(_currentPositionID.ToString());
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            DropDownListHelper.Bind(ddl_Position1, PositionManager.GetPositionCategory(), "name", "id");

            int _id=string.IsNullOrEmpty(ddl_Position1.SelectedValue)?0:Convert.ToInt32(ddl_Position1.SelectedValue);

            DropDownListHelper.Bind(ddl_Postion2,PositionManager.GetPositionList(_id),"name","id");

            if (ddl_Postion2.Items.Count > 0)
            {
                ViewState["position"] = ddl_Postion2.SelectedValue;
            }
        }
    }
    protected void ddl_Position1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int _id = string.IsNullOrEmpty(ddl_Position1.SelectedValue) ? 0 : Convert.ToInt32(ddl_Position1.SelectedValue);

        DropDownListHelper.Bind(ddl_Postion2,PositionManager.GetPositionList(_id),"name","id");

        if (ddl_Postion2.Items.Count > 0)
        {
            this.SetCurrentPosition(ddl_Postion2.SelectedValue);
        }
    }
    protected void ddl_Postion2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_Postion2.Items.Count > 0)
        {
            this.SetCurrentPosition(ddl_Postion2.SelectedValue);
        }
    }
    protected void SetCurrentPosition(string _ID)
    {
        if (ViewState["position"] == null)
        {
            ViewState.Add("position", "0");
        }
        ViewState["position"] = _ID;
    }
}
